//index.js
var app = getApp();
var util = require('../../utils/util.js');
Page({
  //定义变量
  data: {
    lxCheckWorkId:'',
    lxCheckWorkName:'',
    lxClazzName:'',
    lxUsernameTea:'',
    classname:'',
    lxUserNumber:'',
    lxUsernameStu:'',
    year:'',
    month:'',
    day:'',
    hour:'',
    minute:'',
    week:'',
    //默认未获取地址
    hasLocation: false,
    latitude: "",
    longitude: "",
    //按钮状态
    state:true,
    submitBtn:false
    //按钮等待
    // loading:true
  },
  //发送签到请求
  signin:function(){
    this.setData({
      state:true
    })
    console.log("globalData.checkId",app.globalData.checkId)
    console.log("app.globalData.UserWechat.lxUserNumber",app.globalData.UserWechat.lxUserNumber)
    wx.request({
      url: app.globalData.serverUrl+'/daka/signin/saveSignin',
      method: "POST",
      data: { 
        lxCheckWorkId:app.globalData.checkId,
        lxUserNumber:app.globalData.UserWechat.lxUserNumber
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log("/daka/signin/saveSignin",res)
        if(res.data.state==200){
          wx.redirectTo({
            url: '/pages/success/success'　　// 成功页面
          })
        }
        else{
          wx.redirectTo({
            url: '/pages/error/error?errorMsg='+res.data.message　　// 失败页面
          })
        }
      }
    })
   
  },
  //获取当前时间的方法
  NowTime:function(){
    var _that = this;
    var nowtime = new Date()
    var week=util.getWeekByDate(new Date())
   var year=nowtime.getFullYear()
   var month=nowtime.getMonth() + 1
   var day=nowtime.getDate()
   var hour=nowtime.getHours()
   var minute=nowtime.getMinutes()
   var minutetwo=minute.toString();
   var minutend=minutetwo[1]? minutetwo : '0' + minute
    _that.setData({

      year:year,
      month:month,
      day:day,
      hour:hour,
      minute:minutend,
      week:week
    })
  },
  //获取当前经纬度并判断
  GetAddress:function(){
    var _that = this;
    var latitudebegin=app.globalData.latitudebegin
    var  latitudeend=app.globalData.latitudeend
    var longitudebegin=app.globalData.longitudebegin
    var longitudeend=app.globalData.longitudeend
    wx.getLocation({
      type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      isHighAccuracy:true,
      highAccuracyExpireTime:'3000',
      //获取经度维度的范围值
      success: function (res) {
        wx.showToast({
          title:'获取当前位置成功！请点击签到！',
          icon:'none',
        })
        //赋值经纬度
        console.log("经度范围开始",latitudebegin)
        console.log(res.latitude)
        console.log("经度范围结束",latitudeend)
        console.log("纬度范围开始",longitudebegin)
        console.log(res.longitude)
        console.log("纬度范围结束",longitudeend)
        if(parseFloat(latitudebegin)<=parseFloat(res.latitude)
        &&parseFloat(res.latitude)<=parseFloat(latitudeend)
        &&parseFloat(longitudebegin)<=parseFloat(res.longitude)
        &&parseFloat(res.longitude)<=parseFloat(longitudeend))
        {
          _that.setData({
            latitude: res.latitude,
            longitude: res.longitude,
            state:false
          })
        }
        else{
          //弹出提示框：禁用按钮
          console.log("超出范围弹框咯！")
          wx.showToast({
            title:'超出签到范围😒😒😒 请重新扫码签到！！！',
            icon:'none',
            duration:3000
          })
        }
      },
      fail:function(res){
        wx.showToast({
          title:'请检查手机GPS是否打开？',
          icon:'none',
          duration:5000
        })
      }
    })
  },
  //定义定时器设置60S变更一次//可能会有BUG  https://www.jianshu.com/p/c8f6e3a4a91f
  startInter : function(){
    var that = this;
    that.data.inter= setInterval(
        function () {
            that.NowTime(); 
        }, 60000);    
  },

  //执行各种方法
  onLoad: function(options) {
    var that=this
    that.setData({
      lxCheckWorkId:app.globalData.checkId,
      lxCheckWorkName:app.globalData.Checkwork.lxCheckWorkName,
      lxUsernameTea:app.globalData.Checkwork.lxUsername,
      lxUsernameStu:app.globalData.UserWechat.lxUsername,
      lxUserNumber:app.globalData.UserWechat.lxUserNumber
    })
    
    console.log("onLoad: function",this.data.lxCheckWorkName);
    this.startInter();
   this.GetAddress();
    this.NowTime();   
  },
  onShow: function() {
    // 页面出现在前台时执行
    
  },
  onReady: function() {
    // 页面首次渲染完毕时执行
  },
  onHide: function() {
    // 页面从前台变为后台时执行
  },
  onUnload: function() {
    // 页面销毁时执行
  },
  onPullDownRefresh: function() {
    // 触发下拉刷新时执行
   
  },
  onReachBottom: function() {
    // 页面触底时执行
  },

  onPageScroll: function() {
    // 页面滚动时执行
  },
  onResize: function() {
    // 页面尺寸变化时执行
  },
  onTabItemTap(item) {
  
  },
  // 事件响应函数
  //收藏按钮
  onAddToFavorites(res){
    return {
      title: '截止v2.13.0|2020-09-07微信官方暂无法屏蔽“收藏”按钮。',
      imageUrl: '/image/timg.jpg',
      query: '',
    }
  }
})